﻿/*****************************************
 * File Name:   GeoFunc.js
 * Author:      Chen Xin
 * Version:     1.0
 * Date:        2006-10-08
 * Description: GIS功能调用
 *****************************************/

var GeoFunc = new Object();

/**
* Function:    GeoFunc.defaultMap
* Author:      Chen Xin
* Version:     1.0
* Date:        2006-10-17
* Description: 实现全图功能
* Params:      No params
*/
GeoFunc.defaultMap = function() {
    
    leftBar = $("leftBar");
    legend = $("legend");
    m_imgMapCanvas = $("imgMapCanvas");
    
    m_hvMinXDef = $("hvMinXDef");
    m_hvMinYDef = $("hvMinYDef");
    m_hvMaxXDef = $("hvMaxXDef");
    m_hvMaxYDef = $("hvMaxYDef");
    
    if (legend.childNodes.length != 0 && legend.childNodes.length != 1) {
        canvasWidth = document.body.clientWidth - leftBar.offsetWidth - 152 - 24;
    } else {
        canvasWidth = document.body.clientWidth - leftBar.offsetWidth - 24;
    }
    
	var mapPos = Position.cumulativeOffset(m_imgMapCanvas);
	
	m_mapViewer = new Map(mapPos[0],
		  mapPos[1],
		  canvasWidth,
		  m_imgMapCanvas.height,
		  m_ExtentWidths,
		  new Rect(m_hvMinXDef.value, m_hvMinYDef.value, m_hvMaxXDef.value, m_hvMaxYDef.value)
		  );
		  
    checkLegend();
        
	submit();
}

/**
* Function:    GeoFunc.startUp
* Author:      Chen Xin
* Version:     1.0
* Date:        2006-10-08
* Description: MapViewer.aspx初始化时地图加载
* Params:      No params
*/
GeoFunc.startUp = function() {

    leftBar = $("leftBar");
    legend = $("legend");
    //dropDownBiz = $("DropDownBiz");
    dropDownBiz = $("DropDownListBIZ");
    planterInfo = $("planterInfo");
    m_imgMapCanvas = $("imgMapCanvas");

    m_txtXCoord = document.getElementById("txtXCoord");
	m_txtYCoord = document.getElementById("txtYCoord");
	
    m_divZoomBox = $("divZoomBox");
    m_divMapBorder = $("divMapBorder");
    m_hvMinX = $("hvMinX");
    m_hvMinY = $("hvMinY");
    m_hvMaxX = $("hvMaxX");
    m_hvMaxY = $("hvMaxY");

    m_hvMapPage = $("hvMapPage");
    
    if (legend.childNodes.length != 0 && legend.childNodes.length != 1) {
        canvasWidth = document.body.clientWidth - leftBar.offsetWidth - 152 - 24;
    } else {
        canvasWidth = document.body.clientWidth - leftBar.offsetWidth - 24;
    }
    
	var mapPos = Position.cumulativeOffset(m_imgMapCanvas);
	
	m_mapViewer = new Map(mapPos[0],
			  mapPos[1],
			  canvasWidth,
			  m_imgMapCanvas.height,
			  m_ExtentWidths,
			  new Rect(m_hvMinX.value, m_hvMinY.value, m_hvMaxX.value, m_hvMaxY.value)
			  );
	
	m_zbxZoom = new ZoomBox(m_divZoomBox);
	
	m_txtXCoord.value = "经度:";
	m_txtYCoord.value = "纬度:";

	// 定义事件句柄
    if (m_sClientBrowserType == 'Netscape') {
		document.captureEvents(Event.MOUSEDOWN | Event.MOUSEMOVE | Event.MOUSEUP);
	}
	
	Event.observe(document, "mousedown", GeoFunc.handleMouseDown, true);
	Event.observe(document, "mousemove", GeoFunc.handleMouseMove, true);
	Event.observe(document, "mouseup", GeoFunc.handleMouseUp, true);

	
	m_imgMapCanvas.onload = hideWaitImage;
	
    // 请求地图
    submit();
}

// *******************************************************************
// *********************** EVENT HANDLERS  ***************************
// *******************************************************************

/**
* Function:    GeoFunc.handleMouseDown
* Author:      Chen Xin
* Version:     1.0
* Date:        2006-10-08
* Description: 鼠标点击的事件响应
* Params:      evt
*/
GeoFunc.handleMouseDown = function(evt) {
	var evt = evt || window.event;
	
	var eventX;
	var eventY;
	
	
	eventX = mouseCoords(evt).x;
	eventY = mouseCoords(evt).y;

	// Check to see if click is over map
	if (!(m_mapViewer.isEventOverMap(eventX, eventY))) {
		return;
	}

	switch(m_iToolMode) {
		case 1:
			if ((m_sClientBrowserType == 'IE') && (m_sClientPlatform == 'Windows')) {
				m_imgMapCanvas.setCapture();
			}
			m_zbxZoom.show();
			m_zbxZoom.start(eventX, eventY);
			break;
		case 2:
			if ((m_sClientBrowserType == 'IE') && (m_sClientPlatform == 'Windows')) {
				m_imgMapCanvas.setCapture();
			}
			m_zbxZoom.show();
			m_zbxZoom.start(eventX, eventY);
			break;
		case 3:
			if ((m_sClientBrowserType == 'IE') && (m_sClientPlatform == 'Windows')) {
				m_imgMapCanvas.setCapture();
			}
			m_zbxZoom.start(eventX, eventY);

			break;
		case 5:
            //if (dropDownBiz.value != "0")  {
                m_imgMapCanvas.onclick = RequestLoader.showPlanterInfo;
            //}
            break;
        case 6:
            break;
	}
}

/**
* Function:    GeoFunc.handleMouseMove
* Author:      Chen Xin
* Version:     1.0
* Date:        2006-10-08
* Description: 鼠标移动的事件响应
* Params:      evt
*/
GeoFunc.handleMouseMove = function(evt) {
	var evt = evt || window.event;
	
	var eventX;
	var eventY;
	
	
	eventX = mouseCoords(evt).x;
	eventY = mouseCoords(evt).y;

	if (!(m_mapViewer.isEventOverMap(eventX, eventY))) {	
		return;
	}
	//guodanhuai 2007-5-4添加,获取地图比例尺
	var ptStart=m_mapViewer.toMapPoint(0, 0);
	var ptEnd=m_mapViewer.toMapPoint(800, 500);
	var sc =  Math.round((ptStart.y - ptEnd.y) *777000);
	 
	reportCoords(m_mapViewer.toMapPoint(eventX, eventY));
	//guodanhuai 2007-5-4添加,获取地图比例尺
	window.status ="比例尺 1:"  + sc + " | "  +  m_txtXCoord.value + " | " + m_txtYCoord.value;

    if (m_iToolMode == 1 || m_iToolMode == 2 || m_iToolMode == 3) {
	    if (m_zbxZoom.isInProgress) {
		    m_zbxZoom.update(eventX, eventY);
	    }
    }
}

/**
* Function:    GeoFunc.handleMouseUp
* Author:      Chen Xin
* Version:     1.0
* Date:        2006-10-08
* Description: 鼠标键放开的事件响应
* Params:      evt
*/
GeoFunc.handleMouseUp = function(evt) {
	
	//**
	//* Add by Yang xiaodong
	//* Use to Pan tools function
	//* get mouse up event coordinates
	//* 2006-11-23
	//*******************************************
	var evt = evt || window.event;
	var eventX;
	var eventY;
	var newLeft;
	var newTop;	
	var CenterX;
	var CenterY;
	var Scale;
	
    eventX=mouseCoords(evt).x;
    eventY=mouseCoords(evt).y;
    
	if (!(m_mapViewer.isEventOverMap(eventX, eventY))) {	
		return;
	} 
	//***************************************************
		
	if (!(m_iToolMode == 1 || m_iToolMode == 2 || m_iToolMode == 3 )) {
		return;
	}
	if (!(m_zbxZoom.isInProgress())) {
		return;
	}
	// Release the capture that was set in the mousedown
	// handler (again, this is something that we have to do because
	// of drag events in IE).
	if ((m_sClientBrowserType == 'IE') && (m_sClientPlatform == 'Windows')) {
		document.releaseCapture();
	}

    // Stop the current effect.
	m_zbxZoom.stop();
	
	if (m_iToolMode == 1) {
	    // If the width of the zoom box is less than five pixels,
	    // treat is as a single click and zoom in one map level.
	    // Otherwise, set the extent to the zoom box.
//    	if (!(m_zbxZoom.isInProgress())) {
//		    return;
//	    }
//	    m_zbxZoom.stop();
	    if ((Math.abs(m_zbxZoom.getStartX() - m_zbxZoom.getEndX()) < 5) && (Math.abs(m_zbxZoom.getStartY() - m_zbxZoom.getEndY()) < 5))  {	
		    var ptUserClick = m_mapViewer.toMapPoint(m_zbxZoom.getStartX(), m_zbxZoom.getStartY());
		    if (m_mapViewer.getLevel() == 1) {   //if we reach the minimum zoom level, zoom a fixed amount.
		        m_mapViewer.zoom(0.5);
		    } else {
			    m_mapViewer.setLevel(m_mapViewer.getLevel() - 1);
            }
		    m_mapViewer.recenter(ptUserClick);
        } else {
	        borderWidth = 0;
	        if (m_sClientBrowserType == 'Netscape') {
		        borderWidth = 4; // 2 times border width
	        }
	        var ptStart = m_mapViewer.toMapPoint(m_zbxZoom.getStartX()+borderWidth, m_zbxZoom.getStartY()+borderWidth);
	        var ptEnd   = m_mapViewer.toMapPoint(m_zbxZoom.getEndX()+borderWidth, m_zbxZoom.getEndY()+borderWidth);
	        m_mapViewer.setExtent(new Rect(ptStart.x,ptStart.y,ptEnd.x,ptEnd.y));
	       
        }
        checkLegend();
	} else if (m_iToolMode == 2) {
        
	    if ((Math.abs(m_zbxZoom.getStartX() - m_zbxZoom.getEndX()) < 5) && (Math.abs(m_zbxZoom.getStartY() - m_zbxZoom.getEndY()) < 5))  {	
	    var ptUserClick = m_mapViewer.toMapPoint(m_zbxZoom.getStartX(), m_zbxZoom.getStartY());
	    if (m_mapViewer.getLevel() == 11) {   //if we reach the maximum zoom level, zoom a fixed amount.
	        m_mapViewer.zoom(2);
	    } else {
		    m_mapViewer.setLevel(m_mapViewer.getLevel() + 1);
        }
	    m_mapViewer.recenter(ptUserClick);
        } else {
	        borderWidth = 0;
	        if (m_sClientBrowserType == 'Netscape') {
		        borderWidth = 4; // 2 times border width
	        }
		    if (m_mapViewer.getLevel() == 11) {
		        m_mapViewer.zoom(2);
		    } else {
	            var ptStart = m_mapViewer.toMapPoint(m_zbxZoom.getStartX()+borderWidth, m_zbxZoom.getStartY()+borderWidth);
	            var ptEnd   = m_mapViewer.toMapPoint(m_zbxZoom.getEndX()+borderWidth, m_zbxZoom.getEndY()+borderWidth);
	            m_mapViewer.setExtentOut(new Rect(ptStart.x,ptStart.y,ptEnd.x,ptEnd.y));
	        }
        }
        checkLegend();
	}
	else if(m_iToolMode == 3)
	{
        newLeft = m_imgMapCanvas.offsetLeft - (m_zbxZoom.getEndX() - m_zbxZoom.getStartX());
		newTop = m_imgMapCanvas.offsetTop - (m_zbxZoom.getEndY() - m_zbxZoom.getStartY());
		CenterX = newLeft + m_imgMapCanvas.width / 2;
		CenterY = newTop + m_imgMapCanvas.height / 2;
		var click = m_mapViewer.toMapPoint(CenterX,CenterY);
		m_mapViewer.recenter(click);
		
	}
	
	
	submit();
}
